package com.itheima.controller;


import com.itheima.aliyunoss.AliyunUtils;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Setmeal;
import com.itheima.pub.MessageConstant;
import com.itheima.pub.RedisConstant;
import com.itheima.service.SetmealService;
import com.itheima.upload.UploadUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("setmeal")
public class SetmealController {
    @Reference
    private SetmealService setmealService;

    @Autowired
    RedisTemplate redisTemplate;

    @RequestMapping("addmeal")
    public Result uploadFile(@RequestParam("imgFile") MultipartFile file, HttpSession session){
        try {
            // 获取文件在前端上传时的原始名
            String originalFilename = file.getOriginalFilename();
            ServletContext servletContext = session.getServletContext();
            String mimeType = servletContext.getMimeType(originalFilename);
            if (mimeType=="image/jpeg"||mimeType=="image/png"){
            // 生成文件的uuid名
            String uuidFilename = UploadUtils.generateRandonFileName(originalFilename);
            AliyunUtils.uploadMultiPartFileToAliyun(file.getBytes(),"heima109/"+uuidFilename);
            redisTemplate.opsForSet().add(RedisConstant.SETMEAL_PIC_RESOURCES,"heima109/"+uuidFilename);
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,"heima109/"+uuidFilename);
            }else {
                throw new RuntimeException("文件类型错误错误");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    @RequestMapping("add")
    public Result addSetmeal(@RequestBody Setmeal setmeal, @RequestParam("checkgroupIds") Integer[] checkgroupIds){
        try {
            setmealService.addSetmeal(setmeal,checkgroupIds);
            String setmealImg = setmeal.getImg();
            redisTemplate.opsForSet().add(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmealImg);
            return new Result(true, MessageConstant.UPDATE_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.UPDATE_CHECKGROUP_FAIL);
        }
    }

    // 分页查询检查组
    @RequestMapping("findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            Map page = setmealService.findPage(queryPageBean);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,page);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }
}
